﻿Imports System.IO

Partial Class Paginas_InformeConvenio
    Inherits System.Web.UI.Page

#Region "Declaración de Funciones de Página"
    Private Sub cargarcombos()
        Dim miCollectI As New Collection
        Dim miNegRegAdm As New dllNegocio.NegRegiones(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollectI = miNegRegAdm.ListaRegionesVigente()
            Me.ddlRegion.DataSource = miCollectI
            Me.ddlRegion.DataTextField = "Descripcion"
            Me.ddlRegion.DataValueField = "Codigo"
            Me.ddlRegion.DataBind()
            Me.ddlRegion.Items.Insert(0, New ListItem("Todas", "0"))
        Catch ex As Exception
        Finally
            miCollectI = Nothing
            miNegRegAdm.Dispose()
            miNegRegAdm = Nothing
        End Try
    End Sub

    Private Sub CargarInforme()
        Dim miCollect As New Collection
        Dim miNegInf As New dllNegocio.NegInformes(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            Dim FecIni As DateTime
            Dim FecFin As DateTime
            If IsDate(Me.txtFecIni.Text) = True Then
                FecIni = CDate(Me.txtFecIni.Text).ToString("yyyy-MM-dd")
            Else
                FecIni = CDate("1900-01-01")
            End If
            If IsDate(Me.txtFecFin.Text) = True Then
                FecFin = CDate(Me.txtFecFin.Text).ToString("yyyy-MM-dd")
            Else
                FecFin = CDate("2050-12-31")
            End If


            Dim CodTipoAsistente As Integer = Me.ddlRegion.SelectedItem.Value
            miCollect = miNegInf.ListaInformePorConvenio(FecIni, FecFin, CodTipoAsistente)
            Me.GrillaResultados.DataSource = miCollect
            Me.GrillaResultados.DataBind()
            If miCollect.Count > 0 Then Me.btnExportar.Visible = True
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegInf.Dispose()
            miNegInf = Nothing
        End Try
    End Sub
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Call cargarcombos()
            Me.btnExportar.Visible = False
        End If
    End Sub

    Protected Sub btnGenerar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerar.Click
        Call CargarInforme()
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportar.Click
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        Me.GrillaResultados.EnableViewState = False
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(Me.GrillaResultados)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Informe Por Convenio.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub
End Class
